﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGXEngine
{
    public class Command_Function : ICommand
    {
        public int FunctionAddress { get; set; }

        #region ICommand Members

        public bool IsReturn { get { return false; } }
        public bool IsIf { get { return false; } }

        public List<ICommand> ChildrenCommands
        {
            get { return _childrenCommands; }
            set { _childrenCommands = value; }
        }
        private List<ICommand> _childrenCommands = new List<ICommand>();

        public IVariableType Execute(ScriptManager sm, FunctionScope scope)
        {
            Function f = sm.DataTree.FunctionAddressIndex[this.FunctionAddress];

            f.Scope.Reset();

            if (f.Arguments.Count > 0)
            {
                int idx = 0;
                foreach (var c in this.ChildrenCommands) // this should be in the same order as f.Arguments
                {
                    IVariableType vt = f.Arguments[idx++];

                    vt.SetValue(c.Execute(sm, scope));

                    f.Scope.Arguments.Add(vt);
                }
            }

            return f.Execute(sm);
        }

        #endregion
    }
}
